package com.es.disped.wechat;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.alibaba.fastjson.JSONObject;
import com.es.disped.api.http.HttpService;
import com.es.disped.common.wechat.http.config.WeChatConfig;
import com.es.disped.common.wechat.http.request.AccessRequest;
import com.es.disped.common.wechat.http.response.AccessResponse;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={
		"classpath:spring/applicationContext.xml",
		"classpath:spring/springMVC-servlet.xml",
})
public class Token {

    //@Autowired
    private WeChatConfig config=new WeChatConfig();
    
    @Autowired
    HttpService httpService;
    
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?";
    
    
    @Test
    public void testConfig() {
        System.out.println("config:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+config);
    }
    
    @Test
    public void testGetAccessToken() {
        AccessRequest request = new AccessRequest(WeChatConfig.getAppId(), WeChatConfig.getAppsecret());
        // 构建请求参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("grant_type", request.getGrant_type());
        map.put("appid", request.getAppid()); 
        map.put("secret", request.getSecret());
        try {
            String result = httpService.doGet(ACCESS_TOKEN_URL, map);
            AccessResponse response=JSONObject.parseObject(result, AccessResponse.class);
            System.out.println("response:>>>>>>>>>>>>>>>>>>>>>>>>>>>"+response);
            System.out.println(response.getErrmsg());

            System.out.println(response.getErrorcode()==null);
        } catch (Exception e) {
           
            e.printStackTrace();
        }
    }
}
